/* eslint-disable */
/**
/**
 * Created by GYL on 2018/7/25
 */
//知识库
import React, { Component } from 'react';
import MyModal from '../../../components/common/MyModal';
import { Collapse, Icon, Select, Input, Popconfirm, Tooltip, Form, Row, Col, TreeSelect, Tag, Upload, Button, Popover,Steps, Divider, Descriptions } from 'antd';
import Util from '../../../utils/Util'; 
import MyIcon from '../../../components/common/MyIcon';
import config from '../../../config';
import PublicService from "../../../services/PublicService";
import moment from 'moment';
import request from '../../../utils/request';
import MyTable from "../../../components/common/MyTable";
import { message } from 'antd'; 
import Add_zs from './Add_zs';
import Edit_zs from './Edit_zs'; 
const { Step } = Steps;
const FormItem = Form.Item;
const { Panel } = Collapse;
const { Option } = Select;
const { TextArea } = Input;
const { SHOW_CHILD } = TreeSelect;
const IconFont = Icon.createFromIconfontCN({
    scriptUrl: '//at.alicdn.com/t/font_1315525_kt2bd7ytf.js',
});


class SubZS extends Component {
    state = {
        editId: '',
        Edit_items: [],
        dataSource_Add: [],//新增
        dataSource: [],//知识库
        dataSource_Edit: [],//编辑
        xinzengzsk: false,//知识库是否显示
        pid: '',
        add_zs: false,
        spinLoading:false
    };
    componentDidMount() {

        this.findKnowledgeBase()



    };

    //查询知识库列表
    findKnowledgeBase = () => {
        let params = {

            eventBasicId: this.props.eventBasicId,
            page: 1,
            rows: 99999
        }
        request({
            url: `/pm-web/projectmanage/knowledgebase/findKnowledgeBase`,
            method: 'POST',
            data: params
        }).then(data => {
            // console.log(data)
            if (data.rc === 0) {
                this.setState({
                    dataSource: data.ret.list
                })
            }
        })
    }

    //点击新增按钮获取uuid
    addClick = () => {
        this.setState({
            type: '新增',
            xinzengzsk: true,
            add_zs: true,
        })
    }





    // modal保
    onModalSave = (val, pid) => {
        // console.log(val)
        this.setState({
            spinLoading:true
        })
        if (this.state.type === '新增') {
            let params = {
                content: val.content,
                eventBasicId: this.props.eventBasicId,
                id: val.pid,
                repositoryName: val.repositoryName,
                theHeir: PublicService.getCookie('nickName')
            }
            request({
                url: `/pm-web/projectmanage/knowledgebase/insertKnowledgeBase`,
                method: 'POST',
                data: params
            }).then(data => {
                // console.log(data)
                if (data.rc === 0) {
                    message.success('新增成功!')
                    this.findKnowledgeBase()
                }else{
                    message.warning(data.err)
                }
                this.setState({
                    spinLoading:false,
                    add_zs: false
                })
            })
        }

        if (this.state.type === '编辑') {
            let params = {
                content: val.content,
                eventBasicId: this.props.eventBasicId,
                id: this.state.editId,
                repositoryName: val.repositoryName,
                theHeir: PublicService.getCookie('nickName')
            }
            request({
                url: `/pm-web/projectmanage/knowledgebase/updateKnowledgeBase`,
                method: 'POST',
                data: params
            }).then(data => {
                // console.log(data)
                if (data.rc === 0) {
                    message.success('修改成功!')
                    this.findKnowledgeBase()
                } else {
                    message.warning(`${data.err}`)
                }

                this.setState({
                    spinLoading:false,
                    edit_zs: false
                })
            })
        }


    };





    //删除知识库信息
    deleteKnowledgeBase = (record) => {

        request({
            url: `/pm-web/projectmanage/knowledgebase/deleteKnowledgeBase`,
            method: 'POST',
            data: [record.id]
        }).then(data => {
            if (data.rc === 0) {
                message.success("删除成功")
                this.findKnowledgeBase()
            } else {
                message.warning(data.err)
            }
        })
    }

    //查询知识库详情
    findKnowledgeBaseParticulars = (record) => {

        this.setState({
            editId: record.id,
            edit_zs: true,
            type: '编辑'
        })

    }



    confirm = (record) => {
        this.deleteKnowledgeBase(record)
    }

    // 取消保存
    ModalCancel = (type) => {
        let t = this;
        t.setState({
            [type]: false,
            modalBtnLoading: false
        })
    };

    render() {
        let { getFieldDecorator } = this.props.form;
        let t = this;
        const columns = [
            { title: '知识库名称', dataIndex: 'repositoryName', key: 'platform', width: 100 },
            { title: '上传人', dataIndex: 'theHeir', key: 'uploadUser', width: 120 },
            {
                title: '上传时间',
                width: '20%',
                dataIndex: 'createTime',
                align: "center",
                render: (text, record) => {
                    return <span>
                        {record.createTime ? moment(record.createTime).format('YYYY-MM-DD HH:mm:ss') : '无'}
                    </span>

                }
            },
            {
                title: '操作', key: 'operation', width: 100, render: (text, record, index) => <span>
                    <a href="javascript:;" onClick={this.findKnowledgeBaseParticulars.bind(this, record)}>详情</a>
                    <Divider type="vertical" />
                    <Popconfirm
                        title="确定删除此条数据?"
                        onConfirm={this.confirm.bind(this, record)}
                        onCancel={this.cancel}
                        okText="是"
                        cancelText="否"
                    >
                        <a href="javascript:;" >删除</a>
                    </Popconfirm>
                </span>
            },
        ];

        const {spinLoading} = this.state;

        return (

            <div style={{ height: '90%', overflow: 'auto' }}>
                {
                    this.state.add_zs &&
                    <Add_zs
                        mask={true}
                        width={800}
                        visible={this.state.add_zs}
                        title={'知识库'}
                        footerShow={true}
                        destroyOnClose={true}
                        onCancel={this.ModalCancel.bind(this, 'add_zs')}
                        onModalSave={this.onModalSave}
                        spinLoading={spinLoading}
                        footerTitle={'保存'}
                        columns={this.state.columns}

                    />
                }
                {
                    this.state.edit_zs &&
                    <Edit_zs
                        mask={true}
                        width={800}
                        visible={this.state.edit_zs}
                        title={'知识库'}
                        footerShow={true}
                        destroyOnClose={true}
                        onCancel={this.ModalCancel.bind(this, 'edit_zs')}
                        onModalSave={this.onModalSave}
                        footerTitle={'保存'}
                        editId={this.state.editId}
                        modalBtnLoading={spinLoading}
                    />
                }
                {/* <Steps>
                    <Step status="finish" title="Login" icon={<Icon type="user" />} />
                    <Step status="finish" title="Verification" icon={<Icon type="solution" />} />
                    <Step status="process" title="Pay" icon={<Icon type="loading" />} />
                    <Step status="wait" title="Done" icon={<Icon type="smile-o" />} />
                </Steps> */}
                <Button size="small" type="primary" onClick={this.addClick} style={{ marginBottom: 10 }}>新增知识库</Button>

                <MyTable
                    pagination={false}
                    columns={columns}
                    dataSource={this.state.dataSource}
                />


            </div>

        )
    } 
}
export default Form.create()(SubZS);
